<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/24
 * Time: 11:21
 */

namespace app\api\controller;

use app\api\model\ReceiveTask;
use app\api\model\SendCircle;
use app\api\model\Task as TaskModel;
use app\api\model\User as UserModel;
use app\api\service\Task as TaskService;
use think\Db;

class Crontab
{
    //每天执行
    public function day()
    {

    }

    //每周执行
    public function week()
    {

    }

    //重置每日发布任务数量
    public function resetTaskTime()
    {
        UserModel::where('task_time', '<>', 0)->update(['task_time' => 0]);
        echo (new UserModel())->getLastSql();
    }

    //每周权重分红
    public function handleBonusWeight()
    {
        set_time_limit(0);
        //获取分红池金额
        $bonus_pool = Db::name('config_param')->where('key', 'bonus_pool')->value('value');

        //获取总权重
        $weight = UserModel::where('bonus_weight', '<>', 0)->sum('bonus_weight');
        if ($weight == 0) {
            return;
        }

        $bonus_pool = $bonus_pool + 100;
        $weight = 236;

        $each_weight = floor($bonus_pool * 100 / $weight) / 100;

        $week_start = date('Y-m-d', strtotime('last Wednesday'));
        $week_end = date('Y-m-d', strtotime('Tuesday'));
        $title = $week_start . '至' . $week_end;
        $start = strtotime('2018-12-19');
        $cycle = floor((time()-$start)/3600/24/7);

        $week_data = [
            'cycle' => $cycle,
            'title' => $title,
            'bonus_pool' => $bonus_pool,
            'total_weight' => $weight,
            'each_weight' => $each_weight,
            'add_time' => time(),
        ];
        Db::name('week_bonus')->insert($week_data);


        //获取每位推广员的权重
//        $lists = UserModel::where('bonus_weight', '<>', 0)->field('*')->select();
        $lists = UserModel::all(['bonus_weight' => ['>', 0]]);
        $a = 0;
        $b = 0;
        foreach ($lists as $user) {
            $amount = $each_weight * $user['bonus_weight'];
            if ($amount == 0) {
                $b ++;
                continue;
            }
            $user->money = $user['money'] + $amount;
            $user->total_money = $user['total_money'] + $amount;
            $res = $user->save();
            if ($res) {
                //记录账户
                $data = [
                    'title' => '擂台赛每周分红',
                    'user_id' => $user['id'],
                    'task_id' => '',
                    'type' => 1,
                    'amount' => $amount,
                    'add_time' => time(),
                    'task_type' => 3, //擂台赛
                    'status'=>1,//正常 2-待结算 3-冻结
                ];
                Db::name('money')->insert($data);
            }
            $a ++;
        }

        //重置每周权重
//        $this->resetBonusWeight();
        UserModel::where('bonus_weight', '<>', 0)->update(['bonus_weight' => 0]);

        //清空奖金池
        Db::name('config_param')->where('key', ['=', 'pool_base'], ['=', 'weight_base'], ['=', 'bonus_pool'], 'or')->setField('value', 0);
        echo 'SUCCESS';

    }

    //任务驳回未确认,接收任务未确认
    public function taskConfirm()
    {
        set_time_limit(0);
        $time = strtotime('Yesterday');//时间小于昨天的接收任务
        $time = 1545732000;
        //接收任务到时间自动确认,奖励
        ReceiveTask::whereTime('add_time', '<', $time)->where('status', config('status.接收任务_待确认'))->chunk(100, function ($list) {
            $a = 0;
            foreach ($list as $item) {
                //把状态改成已确认
                $item->status = config('status.接收任务_已结算');
                $item->save();
                //分红
                //任务分红
                TaskService::reward($item['user_id'], $item['task_id']);//增加任务完成次数,在分红里面
                TaskModel::where('id', $item['task_id'])->setInc('finish');//增加任务完成人数
                $a++;
            }
            echo $a,'<br>';
            //统计已完成的任务
            TaskModel::where('status', config('status.任务_未完成'))->where('need = finish')->setField('status', config('status.任务_已完成'));
        }, 'add_time', 'asc');
        //任务驳回到时间自动确认,释放任务数,累计驳回次数
        /*ReceiveTask::whereTime('reject_time', '<', $time)->where('status', config('status.接收任务_已驳回'))->chunk(100, function ($list) {
            $b = 0;
            foreach ($list as $item) {
                //把状态改成已驳回处理
                $item->status = config('status.接收任务_驳回处理');
                $item->save();
                TaskModel::where('id', $item['task_id'])->setDec('receive');
                UserModel::where('id', $item['user_id'])->setInc('reject');
                $b++;
            }
            echo $b;
        }, 'add_time', 'asc');*/
        //统计已完成的任务
//        TaskModel::where('status', config('status.任务_未完成'))->where('need = finish')->setField('status', config('status.任务_已完成'));

    }

    //分享任务自动执行
    public function shareTask(){
        set_time_limit(0);
        $time = time() - 3*60;
        SendCircle::where('add_time','<',$time)->where('status',0)->chunk(500,function ($list){
            $b = 0;
            foreach ($list as $item) {
                $item->status = 1;
                $item->save();
                TaskService::reward($item['user_id'],$item['share_no'],false);
//                (new Share())->reward($item['user_id'],$item['share_no']);
                $b++;
            }
            echo $b;
            exit;
        },'add_time', 'asc');
    }
}